Necessary packages included in the project.
library(rvest)
library(sf)
library(dplyr)
library(leaflet)
library(htmlwidgets)
Files with .zip extension are extracted into files.
#unzip the contents in './data/turkey_administrativelevels0_1_2.zip' and save unzipped content in './data/turkey_administrativelevels0_1_2'
unzip(zipfile = "./data/zip/turkey_administrativelevels0_1_2.zip", exdir = "./data/turkey_administrativelevels0_1_2")
#unzip the contents in './data/turkey_centeralpoints_1_2.zip' and save unzipped content in './data/turkey_centralpoints_1_2'
unzip(zipfile = "./data/zip/turkey_centeralpoints_1_2.zip", exdir = "./data/turkey_centralpoints_1_2")
Data with sf structure was obtained with the st_read function.
tur_polbnda_adm1_sf= st_read("data/turkey_administrativelevels0_1_2/tur_polbnda_adm1.shp")
## Reading layer `tur_polbnda_adm1' from data source
## `C:\Users\turab\Documents\R\homework2\data\turkey_administrativelevels0_1_2\tur_polbnda_adm1.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 81 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 25.66851 ymin: 35.80842 xmax: 44.81793 ymax: 42.10479
## Geodetic CRS: WGS 84
tur_pntcntr_adm1_sf = st_read("data/turkey_centralpoints_1_2/tur_pntcntr_adm1.shp")
## Reading layer `tur_pntcntr_adm1' from data source
## `C:\Users\turab\Documents\R\homework2\data\turkey_centralpoints_1_2\tur_pntcntr_adm1.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 81 features and 8 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 26.40629 ymin: 36.19888 xmax: 44.0455 ymax: 42.02687
## Geodetic CRS: WGS 84
The web page with the specified url has been captured for Web Scraping process.
url = "https://covid19.saglik.gov.tr/"
html = read_html(url)
Data were obtained from the table on the web page with the help of various functions.
table_data = html %>%
html_elements("table") %>%
.[[1]] %>%
html_table()
A conversion operation was performed for a variable in the data named table_data.
#The variable named Sayı has been converted from character data type to numeric data type.
table_data$Sayı = as.numeric(gsub(",",".", table_data$Sayı))
A column named Category has been added to table_data.
#The column named Category, which has an ordered factor structure,
# is filled with values corresponding to the specified conditions.
table_data = table_data %>%
mutate(Category = case_when(0<=Sayı & Sayı<=51 ~ "1",
51<Sayı & Sayı<=150 ~ "2",
150<Sayı & Sayı<=200 ~ "3",
200<Sayı & Sayı<=250 ~ "4",
250<Sayı ~ "5")) %>%
mutate(Category = factor(Category,
levels=c("1",
"2",
"3",
"4",
"5"),
ordered = TRUE))
A sub-data including the names of the provinces in Turkey and the geometry features of these provinces was created.
#The variable containing the names of the provinces has been renamed to "İl Adı".
geometry_of_provinces = tur_polbnda_adm1_sf %>%
select(adm1_tr) %>%
rename("İl Adı" = "adm1_tr")
Necessary changes have been made so that the names of the provinces in the table_data and geometry_of_provinces are in the same order.
table_data[c(48,49,50,51,68,69,70,71,72),] = table_data[c(51,48,49,50,71,68,69,72,70),]
The spelling of city names in table_data and geometry_of_provinces has been made the same.
geometry_of_provinces[,"İl Adı"] = table_data[,"İl Adı"]
By merging the table_data and geometry_of_provinces, the data to be used in the visualization of the map is obtained.
map_data = merge(geometry_of_provinces,table_data)
Labels that will appear when hovering over provinces on the map have been created.
labels <- sprintf("<strong>%s</strong><br/>
<strong> %s</strong>", map_data$"İl Adı", map_data$Sayı) %>%
lapply(htmltools::HTML)
A sequential color palette was created with colors corresponding to the specified levels.
#Colors are taken from the map requested from us for each level specified with the help of the colorzilla plugin in Chrome.
pal_col = colorFactor(palette = c("#DAD9D7","#D1D0CE","#9C9C9C","#858585","#7A7878"), levels = c("1","2","3","4","5") ,ordered = TRUE)
A map was created using the data named map_data.
map = map_data %>%
leaflet() %>%
addPolygons(color = "white",
weight = 1,
fillColor = ~pal_col(Category),
fillOpacity = 1,
smoothFactor = 1,
label = labels,
labelOptions = labelOptions(style = list("color" = "white", #for popup label
"background-color" = "#1B888C",
"border-color" = "#1B888C",
"padding" = "20px"),
textsize = "15px",
direction = "auto"),
highlight = highlightOptions(weight = 1,
fillColor = "#0E5C59",
bringToFront = TRUE)) %>%
addLabelOnlyMarkers(lng = tur_pntcntr_adm1_sf$longitude,
lat = tur_pntcntr_adm1_sf$latitude,
label = ~Sayı,
labelOptions = labelOptions(noHide = T,
direction = "center",
textsize = "9px",
style = list("color" = "white",
"background-color" = "#0E5C59",
"border-color" = "#0E5C59",
"border-radius" = "35%",
"padding" = "0px"))) %>%
addLegend(position = "bottomright",
pal = pal_col,
values = ~ Category,
title = 'İllere göre Haftalık Vaka Sayısı (100 binde)')
map